package com.st.buy.manager.controller;

import com.st.buy.manager.service.ItemService;
import com.st.buy.pojo.TbItem;
import com.st.buy.pojo.TbItemDesc;
import com.st.buy.util.pojo.BuyResult;
import com.st.buy.util.pojo.EasyUIDatagrideVO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 张双虎
 */
@RestController //表示这个controller中的请求处理方法返回的结果都是直接输出到客户端
public class ItemController {
    @Autowired
    private ItemService itemService;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("/item/save")
    public BuyResult save(TbItem item,String desc){
        BuyResult result = itemService.save(item,desc);
        //发送消息
        rabbitTemplate.convertAndSend("item-add-exchange",null,(item.getId()+"").getBytes());
        return result;
    }

    @RequestMapping(value = "/item/list",produces = "application/json;charset-utf-8")
    public EasyUIDatagrideVO<TbItem> queryByPage(@RequestParam(defaultValue = "1") long page,@RequestParam(defaultValue = "30") long rows){
        return itemService.queryByPage(page,rows);
    };

    @RequestMapping(value = "/queryById",produces = "application/json;charset=utf-8")
    @ResponseBody
    public TbItem queryById(long itemId){
        return itemService.queryById(itemId);
    }


    @RequestMapping(value = "/queryDescById",produces = "application/json;charset=utf-8")
    @ResponseBody
    public TbItemDesc queryDescById(long itemId){
        return itemService.queryDescById(itemId);
    }

    @RequestMapping(value = "/queryAll",produces = "application/json;charset=utf-8")
    @ResponseBody
    public List<TbItem> queryAll(){
        return itemService.queryAll();
    }
}
